package setup;

import utils.DBConnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-8-19
 * Time: 21:07:18
 */
public class KmSetting {

  public static int addSetting(Connection con, KmSettingInfo info) throws SQLException {
    String sql = "INSERT INTO kmsetting VALUES (NULL, ?, ?, ?, ?, ?, ?)";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setInt(1, info.getKmID());
    ps.setInt(2, info.getPassScore());
    ps.setInt(3, info.getExamTime());
    ps.setInt(4, info.getSValue());
    ps.setInt(5, info.getMValue());
    ps.setInt(6, info.getTValue());
    ps.executeUpdate();
    return DBConnection.getLastID(con);
  }

  public static void delSetting(Connection con, int kmID) throws SQLException {
    String sql = "DELETE FROM kmSetting WHERE kmID=" + kmID + " LIMIT 1";
    con.createStatement().executeUpdate(sql);
  }

  public static KmSettingInfo getSetting(Connection con, int kmID) throws SQLException {
    String sql = "SELECT * FROM kmsetting WHERE kmID=" + kmID + " LIMIT 1";
    ResultSet rs = con.createStatement().executeQuery(sql);
    return rs.next() ? rs2Info(rs) : new KmSettingInfo();
  }

  private static KmSettingInfo rs2Info(ResultSet rs) throws SQLException {
    KmSettingInfo info = new KmSettingInfo();
    info.setKsID(rs.getInt("ksID"));
    info.setKmID(rs.getInt("kmID"));
    info.setPassScore(rs.getInt("passScore"));
    info.setExamTime(rs.getInt("examTime"));
    info.setSValue(rs.getInt("sValue"));
    info.setMValue(rs.getInt("mValue"));
    info.setTValue(rs.getInt("tValue"));
    return info;
  }
}
